package com.qf.fmall.controller;

import com.qf.fmall.entity.Users;
import com.qf.fmall.service.UserService;
import com.qf.fmall.vo.ResStatus;
import com.qf.fmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王权富贵
 */
@RestController
@Api(value = "提供用户的登录和注册接口", tags = "用户管理")
@RequestMapping(value = "/user")

public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @CrossOrigin
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户登录密码", required = true)
    })
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ResultVo login(String username, String password) {
        ResultVo resultVo = userService.getUserByUserName(username, password);
        logger.info(resultVo.getMsg());
        return resultVo;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户注册账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户注册密码", required = true)
    })
    @CrossOrigin
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    public ResultVo register(@RequestBody Users users) {
        System.out.println(users);
        ResultVo resultVo = userService.insertUsers(users);
        return resultVo;
    }

    @CrossOrigin
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public ResultVo checkUser() {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(ResStatus.OK);
        return resultVo;
    }
}
